package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-22
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述注入
     @Autowired
     private EduCourseDescriptionService eduCourseDescriptionService;

     @Autowired
     private EduCourseService eduCourseService;

     //注入小节和章节service
     @Autowired
     private EduVideoService eduVideoService;


     @Autowired
     private EduChapterService eduChapterService;


     //添加课程基本信息的方法
     @Override
     public String saveCourseInfo(CourseInfoVo courseInfoVo) {
         //1，向课程表添加课程基本信息
         //CourseInfoVo对象转换为eduCourse对象

         EduCourse eduCourse = new EduCourse();
         //将表单的数据复制到eduCourse表中，
         BeanUtils.copyProperties(courseInfoVo, eduCourse);
         int insert = baseMapper.insert(eduCourse);
         if (insert == 0) {
             //添加失败
             //throw new GuliException(20001,"添加课程信息失败");
             System.out.println("添加失败...");
         }

         //获取添加之后课程id
         String cid = eduCourse.getId();

         //2,向课程简介表添加课程简介
         //edu_course_description
         EduCourseDescription eduCourseDescription = new EduCourseDescription();
         eduCourseDescription.setDescription(courseInfoVo.getDescription());
         //设置描述idj就是课程id
         eduCourseDescription.setId(cid);
         boolean resultDescription = eduCourseDescriptionService.save(eduCourseDescription);
         if(!resultDescription){
               System.out.println("课程详情信息保存失败");
           }
          return eduCourse.getId();
     }

     //根据课程id,查询课程信息
     @Override
     public CourseInfoVo getCourseInfo(String courseId) {
         //理解，CourseInfoVo = eduCourse +描述
         //1,查课程表
         QueryWrapper queryWrapper = new QueryWrapper();
         queryWrapper.eq("courseId",courseId);
         EduCourse eduCourse = baseMapper.selectById(courseId);
         CourseInfoVo courseInfoVo = new CourseInfoVo();
         BeanUtils.copyProperties(eduCourse,courseInfoVo);//这样的话，courseInfoVo里面就会有课程表数据


         //2,查询描述表
         EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
         courseInfoVo.setDescription(courseDescription.getDescription());
         return courseInfoVo;
     }

     //修改课程信息
     @Override
     public void updateCourseInfo(CourseInfoVo courseInfoVo) {

         //1.修改课程表
         EduCourse endCourse = new EduCourse();
         BeanUtils.copyProperties(courseInfoVo,endCourse);
         int update = baseMapper.updateById(endCourse);
         //影响的行数
         if(update ==0){
             System.out.println("修改课程失败");
         }

         //修改描述表
         EduCourseDescription eduCourseDescription = new EduCourseDescription();
         eduCourseDescription.setId(courseInfoVo.getId());
         eduCourseDescription.setDescription(courseInfoVo.getDescription());
         eduCourseDescriptionService.updateById(eduCourseDescription);


     }

     @Override
     public CoursePublishVo publishCourseInfo(String id) {
         //调用mapper
         CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);

         return publishCourseInfo;
     }

     //删除课程
     @Override
     public void removeCourse(String courseId) {
         /**
          * 这个地方删除，得弄懂表设计中的关联关系，实际业务肯定比这复杂、
          */

         //1，根据课程id删除小节
         eduVideoService.removeVideoByCourseId(courseId);


         //2.根据课程id删除章节
         eduChapterService.removeVideoByCourseId(courseId);


         //3.根据课程id删除描述
         eduCourseDescriptionService.removeById(courseId);

         //4,删除课程本身
         int result = baseMapper.deleteById(courseId);
         if(result ==0){
             System.out.println("失败");
         }
     }


     //1,条件查询带分页查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

         QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>();
         //依次判断封装的条件，是否为空，不为空拼接
        //我理解的是这里只考虑你的分类你选择的是什么，不用考虑一级和二级之间的联动，这属于前端？
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){  //一级分类
            queryWrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }

        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){  //二级分类
            queryWrapper.eq("subject_id",courseFrontVo.getSubjectId());
    }
        //条件：最新，关注度，价格   排序
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            //按销量降序
            queryWrapper.orderByDesc("buy_count_sort",courseFrontVo.getBuyCountSort());
        }
        //按最新降序
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            //按销量降序
            queryWrapper.orderByDesc("gmt_create_sort",courseFrontVo.getGmtCreateSort());
        }
        //按价格降序
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            queryWrapper.orderByDesc("price_sort",courseFrontVo.getPriceSort());
        }

        //分页后的结果放到pageRecourse中。
         baseMapper.selectPage(pageCourse,queryWrapper);

        //所有的记录数
        List<EduCourse> records = pageCourse.getRecords();
        long size = pageCourse.getSize();  //每页记录数
        long total = pageCourse.getTotal();//总共多少条数据
        long pages = pageCourse.getPages();//总共多少页
        long current = pageCourse.getCurrent();//当前页
        boolean b = pageCourse.hasNext();//下一页
        boolean b1 = pageCourse.hasPrevious();//上一页

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("items",records);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("current",current);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}


